package com.example.myapplication.utils;



import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.NonNull;

import com.example.myapplication.R;
import com.example.myapplication.databinding.DialogRenameBinding;

public class RenameDialog extends Dialog implements View.OnClickListener{
    private DialogRenameBinding binding;
    //创建点击确定执行的接口函数
    public interface  OnEnsureListener{
        public void onEnsure(String msg);
    }
    OnEnsureListener onEnsureListener;
    //接口实现
    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    public RenameDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=DialogRenameBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.dialogRenameCancel.setOnClickListener(this);
        binding.dialogRenameOk.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_rename_cancel:
                cancel();
                break;
            case R.id.dialog_rename_ok:
                if (onEnsureListener!=null) {
                    String msg = binding.renameEdit.getText().toString().trim();
                    onEnsureListener.onEnsure(msg);
                }
                cancel();
                break;
        }
    }

    /**
     * 设置edittext显示原来的标题名称
     */

    public void setTipText(String oldText){
        binding.renameEdit.setText(oldText);
    }

    /**
     * 对话框宽度和高度一致
     */
    public void setDialogWidth(){
        Window window = getWindow();//得到窗口对象
        //窗口信息参数
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        //获取屏幕的宽度
        Display display = window.getWindowManager().getDefaultDisplay();
        layoutParams.width = display.getWidth();
        layoutParams.gravity = Gravity.BOTTOM;
        //设置窗口背景透明
        window.setBackgroundDrawableResource(android.R.color.transparent);
        //设置窗口参数
        window.setAttributes(layoutParams);
        //自动弹出软键盘
        handler.sendEmptyMessageDelayed(1,100);


    }
    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            InputMethodManager methodManager =
                    (InputMethodManager) getContext().getSystemService(getContext().INPUT_METHOD_SERVICE);
            methodManager.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
            return false;
        }
    });
}
